//
//  RSRuntimeLrarnTableVC.m
//  RSTools
//
//  Created by thinkjoy on 2017/10/18.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "RSRuntimeLrarnTableVC.h"
#import <objc/message.h>
#import "UIButton+ClickBlock.h"
#import "NSObject+KeyValues.h"
#import "TestModel.h"
#import "Monkey.h"
#import "TestClass.h"

@interface RSRuntimeLrarnTableVC ()
{
    NSArray * _dataAry;
    NSDictionary *dictionary;
    
}
@end

@implementation RSRuntimeLrarnTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataAry    =   @[@"动态方法解析",
                      @"给Category添加属性",
                      @"字典与模型互转",
                      @"自动归档",
                      @"objc_msgSend使用",
                      ];
    
    dictionary = @{
                   @"name":@"Dave Ping",
                   @"age":@24,
                   @"phoneNumber":@18718871111,
                   @"height":@180.5,
                   @"info":@{
                           @"address":@"Guangzhou",
                           },
                   @"son":@{
                           @"name":@"Jack",
                           @"info":@{
                                   @"address":@"London",
                                   },
                           }
                   };
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataAry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    cell.textLabel.text =   _dataAry[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * title    =   _dataAry[indexPath.row];
    if ([title isEqualToString:@"动态方法解析"]) {
         [self forwardingTest];
    }else if ([title isEqualToString:@"给Category添加属性"]) {
        [self categoryTest];
    }else if ([title isEqualToString:@"字典与模型互转"]) {
        [self keyValuesTest];
    }else if ([title isEqualToString:@"自动归档"]) {
        [self keyedArchiverTest];
    }else if ([title isEqualToString:@"objc_msgSend使用"]) {
        [self msgSendTest];
    }
}

-(void)categoryTest{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = self.view.bounds;
    [self.view addSubview:button];
    //    button.click = ^{
    //        NSLog(@"buttonClicked");
    //    };
    
    [button setTapActionWithBlock:^{
        NSLog(@"buttonClicked");
    }];
}

-(void)keyValuesTest{
    
    TestModel *model = [TestModel objectWithKeyValues:dictionary];
    NSLog(@"name is %@",model.name);
    NSLog(@"son name is %@",model.son.name);
    
    NSDictionary *dict = [model keyValuesWithObject];
    NSLog(@"dict is %@",dict);
}

-(void)keyedArchiverTest{
    
    TestModel *model = [TestModel objectWithKeyValues:dictionary];
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"test"];
    [NSKeyedArchiver archiveRootObject:model toFile:path];
    
    TestModel *m = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"m.name is %@",m.name);
    NSLog(@"m.son name is %@",m.son.name);
}

-(void)forwardingTest{
    Monkey *monkey = [[Monkey alloc] init];
    ((void (*) (id, SEL)) objc_msgSend) (monkey, sel_registerName("fly"));
}

-(void)msgSendTest{
    TestClass *objct = [[TestClass alloc] init];
    
    ((void (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("showAge"));
    
    ((void (*) (id, SEL, NSString *)) objc_msgSend) (objct, sel_registerName("showName:"), @"Dave Ping");
    
    ((void (*) (id, SEL, float, float)) objc_msgSend) (objct, sel_registerName("showSizeWithWidth:andHeight:"), 110.5f, 200.0f);
    
#if TARGET_IPHONE_SIMULATOR
    NSLog(@"模拟器");
    float f = ((float (*) (id, SEL)) objc_msgSend_fpret) (objct, sel_registerName("getHeight"));
#else
    NSLog(@"真机");
    //真机处理返回类型为浮点数的消息时，可以直接用objc_msgSend
    float f = ((float (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("getHeight"));
#endif
    
    NSLog(@"height is %.2f",f);
    
    NSString *info = ((NSString* (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("getInfo"));
    NSLog(@"%@",info);
}

@end
